﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PetaPoco;

namespace Shop.Home.Repositories
{
    /// <summary>
    /// 购物车数据访问层
    /// </summary>
    public class ShopCarAccess
    {
        private Database DB = new Database("ConnString");
        /// <summary>
        /// 加入购物车
        /// </summary>
        /// <param name="bcinfo"></param>
        /// <returns></returns>
        public object AddShopCar(Models.BuyCar bcinfo)
        {
            return DB.Insert("Shop_BuyCar", "BCID", bcinfo);
        }
        /// <summary>
        /// 获取购物车信息
        /// </summary>
        /// <param name="UID"></param>
        /// <returns></returns>
        public IList<Models.AnyBuyCar> GetBCInfo(int UID)
        {
            Sql sql = Sql.Builder.Append("select Shop_BuyCar.* ,Shop_Wares.WName,Shop_Wares.Url from Shop_BuyCar join Shop_Wares on Shop_BuyCar.WID = Shop_Wares.WID where Shop_BuyCar.UID = @0", UID);
            return DB.Fetch<Models.AnyBuyCar>(sql);
        }
        /// <summary>
        /// 删除购物车里的商品
        /// </summary>
        /// <param name="BCID"></param>
        /// <returns></returns>
        public int DelShopCar(int BCID)
        {
            Sql sql = Sql.Builder.Append("delete from Shop_BuyCar where BCID = @0", BCID);
            return DB.Execute(sql);
        }
        /// <summary>
        /// 删除当前用户购物车里所有的东西
        /// </summary>
        /// <param name="UID"></param>
        /// <returns></returns>
        public int DelAllShopCar(int UID)
        {
            Sql sql = Sql.Builder.Append("delete from Shop_BuyCar where UID = @0", UID);
            return DB.Execute(sql);
        }
        /// <summary>
        /// 根据ID获取购物车信息
        /// </summary>
        /// <param name="BCID"></param>
        /// <returns></returns>
        public Models.BuyCar GetBCInfoByID(int BCID)
        {
            Sql sql = Sql.Builder.Append("select * from Shop_BuyCar where BCID = @0", BCID);
            return DB.FirstOrDefault<Models.BuyCar>(sql);
        }
    }
}
